package me.shan.util.comparators;

import me.shan.sort.heap.HeapSort;

import java.util.Arrays;

/**
 * 描述：排序比较器
 *
 * @author liuchuang
 * @since 2021/6/13
 */
public class SortComparators {

    /**
     * 比较器
     *
     * @param array 目标数组
     */
    private static void compare(int[] array) {
        Arrays.sort(array);
    }

    /**
     * 随机数组生成器
     * @param maxSize 数组最大长度
     * @param maxValue 元素最大值 [-maxValue, maxValue]
     * @return 随机生成的数组
     */
    private static int[] generateRandomArray(int maxSize, int maxValue) {
        // 数组随机大小
        int[] array = new int[(int)(Math.random()*maxSize)];
        for (int i = 0; i < array.length; i++) {
            // 确保元素正负零都有出现的可能
            array[i] = ((int) (Math.random() * (maxValue + 1))) - ((int) (Math.random() * maxValue));
        }
        return array;
    }

    private static int[] copyArray(int[] array) {
        int[] result = new int[array.length];
        System.arraycopy(array, 0, result, 0, array.length);
        return result;
    }

    private static boolean isEqual(int[] array, int[] array1) {
        return Arrays.equals(array, array1);
    }

    public static void main(String[] args) {
        int times = 50000;
        int maxSize = 1000;
        int maxValue = 1000000;
        boolean success = true;
        while (times > 0) {
            int[] array = generateRandomArray(maxSize, maxValue);
            int[] array1 = copyArray(array);
            int[] array2 = copyArray(array);
            compare(array1);
            HeapSort.sort(array2);
            if (!isEqual(array1, array2)) {
                success = false;
                System.out.println("原输入数组：" + Arrays.toString(array));
                System.out.println("系统排序后的的数组：" + Arrays.toString(array1));
                System.out.println("错误排序后的数组：" + Arrays.toString(array2));
                break;
            }
            times--;
        }
        if (!success) {
            System.out.println("有bug，赶紧改");
        } else {
            System.out.println("成功");
        }
    }

}
